package indi.jp.project.business.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import indi.jp.project.business.device.dao.DeviceDispatchDAO;
import indi.jp.project.business.device.entity.dto.DeviceChangeDTO;
import indi.jp.project.business.device.entity.dto.DeviceHistoryDTO;
import indi.jp.project.center.service.TaskService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import indi.jp.project.process.entity.FlowTaskNotifyDTO;
import indi.jp.project.process.service.FlowTaskService;
import indi.jp.project.business.device.entity.DeviceDispatch;
import indi.jp.project.business.device.service.DeviceDispatchService;
import indi.jp.project.business.device.service.DeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

@Service("deviceDispatchService")
public class DeviceDispatchServiceImpl implements DeviceDispatchService {

    @Resource
    private DeviceDispatchDAO deviceDispatchDAO;

    @Resource
    private DeviceService deviceService;

    @Resource
    private TaskService taskService;

    @Resource
    private SerialUtil serialUtil;

    @Resource
    private FlowTaskService flowTaskService;

    @Override
    public JsonResult queryGrid(DeviceDispatch entity) throws Exception {
        List<DeviceDispatch> list = deviceDispatchDAO.queryList(entity);
        int count = deviceDispatchDAO.queryCount(entity);
        Datagrid<DeviceDispatch> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public JsonResult save(DeviceDispatch entity) throws Exception {
        String id;
        if (StringUtils.isBlank(entity.getId())) {
            id = String.valueOf(serialUtil.nextId());
            entity.setId(id);
            deviceDispatchDAO.insert(entity);
        } else {
            id = entity.getId();
            deviceDispatchDAO.update(entity);
        }
        flowTaskService.saveTaskFlow(id, entity.getFlowList());
        return JsonResult.success("保存成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteById(String id) throws Exception {
        deviceDispatchDAO.deleteById(id);
        flowTaskService.deleteFlowByTaskId(id);
        return JsonResult.success("删除成功");
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        DeviceDispatch deviceDispatch = deviceDispatchDAO.findById(id);
        return JsonResult.success(deviceDispatch);
    }

    @Override
    public void notify(FlowTaskNotifyDTO flowTaskNotifyDTO) throws Exception {
        if (flowTaskNotifyDTO.getNextState() == 99) {
            DeviceDispatch deviceDispatch = deviceDispatchDAO.findById(flowTaskNotifyDTO.getTaskId());
            String devices = deviceDispatch.getDevices();
            JSONArray ja = JSONArray.parseArray(devices);
            List<DeviceHistoryDTO> historyList = new ArrayList<>();
            List<DeviceChangeDTO> changeList = new ArrayList<>();
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = ja.getJSONObject(i);
                String deviceId = jo.getString("id");
                String eventDesc = "从" + jo.getString("projectName") + "调度至" + deviceDispatch.getProjectName();
                DeviceHistoryDTO deviceHistoryDTO = new DeviceHistoryDTO();
                deviceHistoryDTO.setId(String.valueOf(serialUtil.nextId()));
                deviceHistoryDTO.setDeviceId(deviceId);
                deviceHistoryDTO.setBillId(deviceDispatch.getId());
                deviceHistoryDTO.setEventType("dispatch");
                deviceHistoryDTO.setEventDesc(eventDesc);
                deviceHistoryDTO.setCreateBy(deviceDispatch.getCreateBy());
                historyList.add(deviceHistoryDTO);

                DeviceChangeDTO deviceChangeDTO = new DeviceChangeDTO();
                deviceChangeDTO.setDeviceId(deviceId);
                deviceChangeDTO.setOperator(deviceDispatch.getCreateBy());
                deviceChangeDTO.setProjectId(deviceDispatch.getProjectId());
                deviceChangeDTO.setProjectCode(deviceDispatch.getProjectCode());
                deviceChangeDTO.setProjectName(deviceDispatch.getProjectName());
                changeList.add(deviceChangeDTO);
            }
            deviceService.updateDevice(changeList);
            deviceService.saveDeviceHistory(historyList);
        }
    }

}
